package com.smec.remes.module.submit.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.acker.simplezxing.activity.CaptureActivity;
import com.google.gson.Gson;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.smec.moblie.R;
import com.smec.moblie.databinding.ActivityPackingBinding;
import com.smec.remes.base.SmecBaseActivity;
import com.smec.remes.bean.NewPackingList;
import com.smec.remes.bean.SubmitDetailsConfirme;
import com.smec.remes.bean.query.Packing;
import com.smec.remes.module.submit.SmecSubmitPresenter;
import com.smec.remes.module.submit.adapter.SmecWeiChildrenAdapter;
import com.smec.remes.smecManager.SmecRetrofit.HttpResult;
import com.smec.remes.util.CommonUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/1/22.
 */

public class SmecPackingActivity extends SmecBaseActivity<SmecSubmitPresenter> {

    private ActivityPackingBinding activityPackingBinding;
    private SmecWeiChildrenAdapter smecWeiChildrenAdapter;
    public static final String SURE="SURE";
    public static final String SUREFAIL="SUREFAIL";
    private ArrayList<SubmitDetailsConfirme> packList=new ArrayList<>();
    public static final String PACKING="PACKING";
    private String confirmNo;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPackingBinding=DataBindingUtil.setContentView(this, R.layout.activity_packing);
        smecWeiChildrenAdapter=new SmecWeiChildrenAdapter(this);
        activityPackingBinding.weishouhuoRecyclerView.setLayoutManager(new LinearLayoutManager(SmecPackingActivity.this));
        activityPackingBinding.weishouhuoRecyclerView.setAdapter(smecWeiChildrenAdapter);
        _submit();
        _return();
        _getList();
    }

    public void _return(){
        activityPackingBinding.searchTopbar.getTvTextLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmecPackingActivity.this.finish();
            }
        });
    }

    public void _getList(){
        Intent intent=getIntent();
        confirmNo=intent.getStringExtra("confirmNo");
        mPresenter._packing(confirmNo);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SURE)
            }
    )

    public void _sureSuccess(String a){
        progressDialog.dismiss();
        if(!TextUtils.isEmpty(a)){
            Toast.makeText(SmecPackingActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
            SmecPackingActivity.this.finish();
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SUREFAIL)
            }
    )

    public void _sureFail(String a){
        progressDialog.dismiss();
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(PACKING)
            }
    )

    public void _packSuccess(HttpResult<NewPackingList> arrayListHttpResult){
        if(CommonUtils.notEmpty(arrayListHttpResult.getData().getConfimrList())){
            for(int i=0;i<arrayListHttpResult.getData().getConfimrList().size();i++){
                SubmitDetailsConfirme submitDetailsConfirme=new SubmitDetailsConfirme();
                submitDetailsConfirme.setDistNo(arrayListHttpResult.getData().getConfimrList().get(i).getDistNo());
                submitDetailsConfirme.setDistUser(arrayListHttpResult.getData().getConfimrList().get(i).getDistUser());
                submitDetailsConfirme.setDistributeDate(arrayListHttpResult.getData().getConfimrList().get(i).getDistTime());
                submitDetailsConfirme.setFlag(arrayListHttpResult.getData().getConfimrList().get(i).getFlag());
                submitDetailsConfirme.setMatnr(arrayListHttpResult.getData().getConfimrList().get(i).getMatnr());
                submitDetailsConfirme.setMatnrName(arrayListHttpResult.getData().getConfimrList().get(i).getMatnrName());
                submitDetailsConfirme.setPkgUnit(arrayListHttpResult.getData().getConfimrList().get(i).getPkgUnit());
                submitDetailsConfirme.setApplyQty(Integer.parseInt(arrayListHttpResult.getData().getConfimrList().get(i).getApplyQty()));
                submitDetailsConfirme.setDistributeQty(Integer.parseInt(arrayListHttpResult.getData().getConfimrList().get(i).getDistributeQty()));
                packList.add(submitDetailsConfirme);
            }
            smecWeiChildrenAdapter.setArrayList(packList);
        }
    }

    public void _submit(){
        activityPackingBinding.btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(confirmNo)){
                    if(progressDialog == null){
                        progressDialog=new ProgressDialog(SmecPackingActivity.this);
                    }
                    progressDialog.setMessage("正在上传.....");
                    progressDialog.show();
                    mPresenter._sure(confirmNo);
                }else {
                    Toast.makeText(SmecPackingActivity.this,"当前没有流水号",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public SmecSubmitPresenter getPresenter() {
        return new SmecSubmitPresenter(this);
    }
}
